package com.myapp.ruyixing.ui;

import static android.view.View.GONE;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Reservation;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class ReservationOldActivity extends BaseActivity {

	RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Reservation reservation = (Reservation) o;
				onLoadingDataFinished(true, reservation);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String bduss, String shopid, String name,
			String mobile, String content) {
		showLoading(true, "加载中");
		RYHttpClient.doActivityReserve(this, mHttpResponseHandler, bduss,
				shopid, name, mobile, content);
	}

	private void onLoadingDataFinished(boolean succeed, Reservation reservation) {
		if (succeed && reservation != null && reservation.errno == 0) {
			showToast("成功提交订单！");
			finish();
		} else {
			showLoading(false, "加载中");
			if (reservation != null && !TextUtils.isEmpty(reservation.msg)) {
				showToast(reservation.msg);
			} else {
				showToast("订单提交失败！");
			}
		}
	}

	public static void startActivityReservationActivity(Context context,
			String activityId) {
		if (context == null || TextUtils.isEmpty(activityId)) {
			return;
		}
		Intent intent = new Intent(context, ReservationOldActivity.class);
		intent.putExtra("shopid", activityId);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reservation_old);
		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		findViewById(R.id.btn_ok).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String bduss = LoginHelper.getInstance().getBDUSS();
				if (TextUtils.isEmpty(bduss)) {
					showToast("登录失效，请重新登录！");
					finish();
					return;
				}
				String shopid = getIntent().getStringExtra("shopid");
				if (TextUtils.isEmpty(shopid)) {
					showToast("无效商家");
					finish();
					return;
				}
				String name = ((EditText) findViewById(R.id.edit_name))
						.getText().toString();
				if (TextUtils.isEmpty(name)) {
					showToast("请输入您的姓名！");
					findViewById(R.id.edit_name).requestFocus();
					return;
				}
				String phone = ((EditText) findViewById(R.id.edit_phone))
						.getText().toString();
				if (TextUtils.isEmpty(phone)) {
					showToast("请输入您的电话！");
					findViewById(R.id.edit_phone).requestFocus();
					return;
				}
				String remark = ((EditText) findViewById(R.id.edit_remark))
						.getText().toString();
				if (TextUtils.isEmpty(remark)) {
					showToast("请输入您的订单备注：预定时间和就餐人数等。");
					findViewById(R.id.edit_remark).requestFocus();
					return;
				}
				onLoadingDataStarted(bduss, shopid, name, phone, remark);
			}
		});
	}

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void showLoading(boolean show, String msg) {
		if (isFinishing()) {
			return;
		}
		if (show) {
			findViewById(R.id.layout_loading).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.text_loading)).setText(msg);
		} else {
			findViewById(R.id.layout_loading).setVisibility(GONE);
		}
	}

}
